<template>
  <div class="registerbox">
    <el-card class="gray-card no-border no-margin-top" shadow="never">
      <el-form ref="form" :model="form" class="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="维护编号：">
              <el-input v-model="form.taskNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仪器编号：">
              <el-input v-model="form.instrumentNo" :disabled="flag"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仪器名称：">
              <el-input v-model="form.instrumentName" :disabled="flag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维护时间：">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                placeholder="选择维护时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="维护人：">
              <el-input v-model="form.maintainer"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维护结果：">
              <el-input v-model="form.result"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="维护记录：">
          <el-input type="textarea" class="textarea"
                    :rows="4"
                    placeholder="请输入内容"
                    v-model="form.remark">
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下一次维护时间：">
              <el-date-picker
                v-model="form.nextService"
                type="datetime"
                placeholder="选择下一次维护时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式：">
              <el-input v-model="form.contact"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属厂商：">
              <el-input v-model="form.manufacturer"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-card>
    <div class="details-foot">
      <div class="cell-btn">
        <el-button type="primary" @click.native="handleSubmit">立即创建</el-button>
        <el-button @click.native="handleClose">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {addServiceTask, updateServiceTask} from "@/api/business/serviceTask"
import {parseTime} from '@/utils/ruoyi'

export default {
  name: "addMaintenance",
  data() {
    return {
      deviceList: [],
      loading: false,
      redirect: undefined,
      registerShow: false,
      dateOptions: [],
      form: {
        taskNo: '',
        maintainer: '',
        createTime: '',
        remark: '',
        nextService: '',
        contact: '',
        instrumentNo: '',
        instrumentName: '',
        result: '',
        manufacturer: ''
      }
    };
  },
  watch: {},
  created() {
    this.form.instrumentId = this.deviceInfo.id
    if (this.flag) {
      this.form.instrumentNo = this.deviceInfo.instrumentNo
      this.form.instrumentName = this.deviceInfo.instrumentName
      this.form.manufacturer = this.deviceInfo.manufacturer
    }
  },
  props: {
    deviceInfo: {
      type: Object,
      default: {}
    },
    flag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    handleData(row) {
      if (row) {
        this.form.id = row.id
        this.form.taskNo = row.taskNo
        this.form.maintainer = row.maintainer
        this.form.createTime = row.createTime
        this.form.remark = row.remark
        this.form.nextService = row.nextService
        this.form.result = row.result
        this.form.contact = row.contact
        this.form.instrumentNo = row.instrumentNo
        this.form.instrumentName = row.instrumentName
        this.form.manufacturer = row.manufacturer
      }
    },
    handleSubmit() {
      this.form.nextService = parseTime(this.form.nextService)
      this.form.createTime = parseTime(this.form.createTime)
      if (this.form.id == null || this.form.id == undefined) {
        addServiceTask(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      } else {
        updateServiceTask(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      }
    },
    handleClose() {
      this.form.id = ''
      this.form.taskNo = ''
      this.form.maintainer = ''
      this.form.createTime = ''
      this.form.remark = ''
      this.form.nextService = ''
      this.form.result = ''
      this.form.contact = ''
      this.form.instrumentName = ''
      this.form.manufacturer = ''
      this.form.instrumentNo = ''
      this.$emit('closeMain', false)
    }
  }
};
</script>
